<!DOCTYPE html>
<html>
    <head>
        <meta charset="ISO-8859-1">
        <title>Insert title here</title>
    </head>
    <body>
        <h1>Adding Methods to JavaScript Objects</h1>
        Methods are just functions attached to objects.
        Defining methods to an object is done inside the constructor function:
        <script>
            function person(firstName, lastName, age, eyeColor) {
                this.firstName = firstName;
                this.lastName = lastName;
                this.age = age;
                this.eyeColor = eyeColor;

                /*this.changeName = changeName;
                 
                 function changeName(name) {
                 this.lastName = name;
                 }*/

                //sau mai clar puteam sa fac asa:

                this.changeName = function(name) {
                    this.lastName = name;
                }

            }

            var myMother = new person("Sally", "Rally", 48, "green");

            document.write(myMother.firstName + " is " + myMother.age + " years old.");

            myMother.changeName("Doe");

            document.write("New Name:" + myMother.lastName);
        </script>
        <h1>JavaScript for...in Loop</h1>
        The JavaScript <code>for...in</code> statement loops through the properties of an object.
        <p id="demo"></p>
        <script>
            var txt = "";
            for (x in myMother) {
                txt += "[" + myMother[x] + "]";
            }
            document.getElementById("demo").innerHTML = txt;
        </script>
        <hr>
        <h2>More examples of the <em>for...in</em> Loop  </h2>
        <script>
            var roles = {
                sysadmin: true,
                ccl: ["CAP0001", "CAP0002"],
                ccld: ["CAP0003"]
            };

            function parseObject(input) {
                var rolesCaps = [];
                for (var oneProperty in input) {
                    if (input.sysadmin && input.sysadmin == true && oneProperty == "sysadmin") {
                        var roleObj = {};
                        console.debug("This is how you test if a proprty is defined in an object");
                        roleObj.role = oneProperty;
                        rolesCaps.push(roleObj);
                    } else {
                        var capabilitiesIDs = input[oneProperty];
                        for (var i = 0; i < capabilitiesIDs.length; i++) {
                            var roleObj = {};
                            roleObj.role = oneProperty;
                            roleObj.capability = capabilitiesIDs[i];
                            rolesCaps.push(roleObj);
                        }
                    }
                }
                return rolesCaps;
            }
        </script>
        <p id="parseObj"></p>
        <button onclick="doStuff()">Click me</button>
        <script>
            function doStuff() {
                var array = parseObject(roles);
                var txt = "";
                for (var index in array) {
                    var item = array[index];
                    if (item.role == "sysadmin") {
                        txt += "[" + item.role + "]";
                    } else {
                        txt += "[" + item.role + "-" + item.capability + "]";
                    }
                }
                document.getElementById("parseObj").innerHTML = txt;
            }
        </script>
        <hr>
        Check for null or empty
        <button onclick="runChecks()">Check for null and Empty</button>
        <script>
            function runChecks(){
                
                var input = {
                    "status": "OK",
                    "packages": {
                        "/capabilityWorkflow/Y9B4AG826/REQ11221149": {
                            "reviewers": [],
                            "recorder": [{
                                "role": "RECORDER",
                                "cnum": "Y9BAXG826",
                                "name": "Gaston, Razvan *CONTRACTOR*",
                                "delegateCnum": "O93344826",
                                "delegateName": "BIRLEA, NICOLAE"
                                }
                            ],
                            "reviewStepId": 0
                        }
                    }
                };
                
                if(input.packages && input.packages["/capabilityWorkflow/Y9B4AG826/REQ11221149"]){
                    var p = input.packages["/capabilityWorkflow/Y9B4AG826/REQ11221149"];
                    if(p.reviewers && p.reviewers.length==0){
                        console.debug("Empty Array");
                    }
                    if(p.recorder && p.recorder.length>0){//Check for null and NonEmpty Array
                        console.debug(p.recorder[0].cnum); 
                    }
                }
                
            }
        </script>
    </body>
</html>